library(ggplot2)
library(plyr)
library(grid)
library(foreign)

rm(list=ls())
setwd("~/Dropbox/Dissertation/Voting Rule/QJPS")

### Figure 1: leader responsiveness
responsiveness <- read.dta("replication files/temp/responsiveness.dta",convert.factors=FALSE)
head(responsiveness)

responsiveness$pe <- responsiveness$estimate
responsiveness$lb <- responsiveness$min90
responsiveness$ub <- responsiveness$max90

responsiveness$group <- factor(responsiveness$idnum,levels=c(6,5,4,3,2,1),
      labels=c("Responsiveness\nSummary index","Member obtaines \nreceipts (M)", "Receipts are given\n to members (R)","Member warned: \nside-selling (M)",
               "Member warned: bad\n  agricultural practices (M)","Manager is very\n transparent (M)")[6:1])

f1 = ggplot(data = responsiveness, aes(x = group, y = pe, ymin = lb, ymax = ub))
f1 = f1 + geom_pointrange(width=.5, size=.7, position = position_dodge(width=.5))
f1 = f1 + scale_shape_manual(values=c(22,22))  # as before
f1 = f1 + scale_fill_manual(values=c("black","black"))
f1 = f1 + coord_flip()
f1 = f1 + ylim(-.5,2)
f1 = f1 + geom_hline(yintercept=0, linetype="longdash", size=.8)
f1 = f1 + labs(x="", y="Regression Coefficient")
f1 = f1 + theme(axis.text.x = element_text(size = rel(.85)))
f1 = f1 + theme(axis.text.y = element_text(size = rel(.85)))
f1 = f1 + theme(axis.title.x = element_text(size = rel(.8)))
f1 = f1 + ggtitle("Leader Responsivness") + theme(plot.title=theme_text(size=12, face="bold"))  
ggsave(file="Figures/responsivenessC.pdf", width=7, height=4)
dev.off()
print(f1)

### Figure 2: Member cooperation
cooperation <- read.dta("replication files/temp/cooperation.dta",convert.factors=FALSE)
head(cooperation)

cooperation$pe <- cooperation$estimate
cooperation$lb <- cooperation$min90
cooperation$ub <- cooperation$max90

cooperation$group <- factor(cooperation$idnum,levels=c(8,7,6,5,4,3,2,1),
                    labels=c("Cooperation \nsummary index", "Member agree to\n increase commission", "Member paid \njoining fees", 
                    "Member paid \nannual dues", "Member's contribution \ncommitment experiment", "Member's share of \ncoffee bulked", 
                     "Member planted \nseedlings", "Member dries \ncoffee on tarps")[8:1])

f2 = ggplot(data = cooperation, aes(x = group, y = pe, ymin = lb, ymax = ub))
f2 = f2 + geom_pointrange(width=.5, size=.7, position = position_dodge(width=.5))
f2 = f2 + scale_shape_manual(values=c(22,22))  # as before
f2 = f2 + scale_fill_manual(values=c("black","black"))
f2 = f2 + coord_flip()
f2 = f2 + scale_y_continuous(breaks=seq(-1,2,1/2))
f2 = f2 + geom_hline(yintercept=0, linetype="longdash", size=.8)
f2 = f2 + labs(x="", y="Regression Coefficient")
f2 = f2 + theme(axis.text.x = element_text(size = rel(.85)))
f2 = f2 + theme(axis.text.y = element_text(size = rel(.85)))
f2 = f2 + theme(axis.title.x = element_text(size = rel(.8)))
f2 = f2 + ggtitle("Member Cooperation") + theme(plot.title=theme_text(size=12, face="bold"))  
ggsave(file="Figures/cooperationC.pdf", width=7, height=5)
dev.off()
print(f2)

### Figure 3: monitoring
monitoring <- read.dta("replication files/temp/monitoring.dta",convert.factors=FALSE)
monitoring

monitoring$pe <- monitoring$estimate
monitoring$lb <- monitoring$min90
monitoring$ub <- monitoring$max90

monitoring$group <- factor(monitoring$idnum,levels=c(10,9,8,7,6,5,4,3,2,1),
  labels=c("DC monitoring \nsummary index", "Council meeting \nfrequency (E)", "Reps influence over \ndecisions (R)", 
  "External auditing (R)", "Internal auditing (R)", "General assembly \n reps attendance (R)", 
   "General assembly: \nmembs' attendance (M)", "Manager is \nmonitored (R)", 
    "Manager is \n monitored (M)", "Manager is \naccountable (M)")[10:1])

f3 = ggplot(data = monitoring, aes(x = group, y = pe, ymin = lb, ymax = ub))
f3 = f3 + geom_pointrange(width=.5, size=.7, position = position_dodge(width=.5))
f3 = f3 + scale_shape_manual(values=c(22,22))  # as before
f3 = f3 + scale_fill_manual(values=c("black","black"))
f3 = f3 + coord_flip()
f3 = f3 + scale_y_continuous(breaks=seq(-.5,2.5,1/2))
f3 = f3 + geom_hline(yintercept=0, linetype="longdash", size=.8)
f3 = f3 + labs(x="", y="Regression Coefficient")
f3 = f3 + theme(axis.text.x = element_text(size = rel(.85)))
f3 = f3 + theme(axis.text.y = element_text(size = rel(.85)))
f3 = f3 + theme(axis.title.x = element_text(size = rel(.8)))
f3 = f3 + ggtitle("Monitoring") + theme(plot.title=theme_text(size=12, face="bold"))  
ggsave(file="Figures/monitoringC.pdf", width=7, height=5.5)
dev.off()
print(f3)
